﻿using Coldairarrow.Entity.Goods;
using Coldairarrow.Entity.Purchasing;
using Coldairarrow.IBusiness;
using Coldairarrow.IBusiness.Purchasing;
using Coldairarrow.Util;
using EFCore.Sharding;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Purchasing
{
    public class BTB_PurchaseBusiness : BaseBusiness<BTB_Purchase>, IBTB_PurchaseBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public BTB_PurchaseBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }
        public async Task<PageResult<BTB_Purchase>> GetDataList(PageInput<BTB_Purchase> input)
        {
            var bTB_Purchase = input.Search;

            var result = GetIQueryable();
            
            //商品id
            if (!string.IsNullOrEmpty(bTB_Purchase.Goodsname))
            {
                result = result.Where(a => a.GoodsSpecificationsItemID == (Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(z => z.GoodsSpecifications.Contains(bTB_Purchase.Goodsname)).FirstOrDefault().GoodsSpecificationsItemID));
            }
            //商品id
            if (bTB_Purchase.State)
            {
                result = result.Where(c => c.State == bTB_Purchase.State);
            }
            else
            {
                result = result.Where(c => c.State == bTB_Purchase.State);
            }
            var where = LinqHelper.True<BTB_Purchase>();

            int count = result.Count();
            var q = await result.Select(s => new BTB_Purchase
            {
                id = s.id,
                GoodsSpecificationsItemID = s.GoodsSpecificationsItemID,
                Count = s.Count,
                State = s.State,
                CreateTime = s.CreateTime,
                EditTime = s.EditTime,
                CreatorId = s.CreatorId,
                Editor = s.Editor,
                Deleted = s.Deleted,
                LackNumber = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(v => v.GoodsSpecificationsItemID == s.GoodsSpecificationsItemID).FirstOrDefault().stock - Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(v => v.GoodsSpecificationsItemID == s.GoodsSpecificationsItemID).FirstOrDefault().Inventory,
                name = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(v=>v.GoodsSpecificationsItemID == s.GoodsSpecificationsItemID).FirstOrDefault().GoodsSpecifications,
                Goodsname = Db.GetIQueryable<BTB_Goods>(true).Where(c=>c.ID == Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(v => v.GoodsSpecificationsItemID == s.GoodsSpecificationsItemID).FirstOrDefault().GoodsID).FirstOrDefault().GoodsName,
            }).Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows)
                .ToListAsync();

            return new PageResult<BTB_Purchase> { Data = q, Total = count };
        }
        public async Task AddDataAsync(BTB_Purchase data)
        {
            BTB_Purchase data1 = new BTB_Purchase()
            {
                id = GuidHelper.GenerateKey(),
                GoodsSpecificationsItemID = data.GoodsSpecificationsItemID,
                Count = data.Count,
                State = data.State,
                CreateTime = data.CreateTime,
                EditTime = data.EditTime,
                CreatorId = _operator.UserId,
                Editor = data.Editor,
                Deleted = data.Deleted,
            };
            await InsertAsync(data1);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task<BTB_Purchase> GetTheDataAsync(string id)
        {
            var aa = GetIQueryable().Where(q => q.GoodsSpecificationsItemID == id && q.State == false);
            if (aa.Count() > 0)
            {
                BTB_Purchase data = new BTB_Purchase()
                {
                    id = aa.FirstOrDefault().id,
                    GoodsSpecificationsItemID = aa.FirstOrDefault().GoodsSpecificationsItemID,
                    Count = aa.FirstOrDefault().Count,
                    State = aa.FirstOrDefault().State,
                    CreateTime = aa.FirstOrDefault().CreateTime,
                    EditTime = aa.FirstOrDefault().EditTime,
                    CreatorId = aa.FirstOrDefault().CreatorId,
                    Editor = aa.FirstOrDefault().Editor,
                    Deleted = aa.FirstOrDefault().Deleted,
                };
                return data;
            }
            else
            {
                BTB_Purchase data = new BTB_Purchase();
                return data;
            }

        }
        public async Task UpdateDataAsync(BTB_Purchase data)
        {
            BTB_Purchase data1 = new BTB_Purchase()
            {
                id = data.id,
                GoodsSpecificationsItemID = data.GoodsSpecificationsItemID,
                Count = data.Count,
                State = data.State,
                CreateTime = data.CreateTime,
                EditTime = data.EditTime,
                CreatorId = data.CreatorId,
                Editor = _operator.UserId,
                Deleted = data.Deleted
            };
            await UpdateAsync(data1);
        }
    }
}
